﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using HidesaburoDotNET.Core;
using System.Windows.Forms;
using System.IO;

namespace HidesaburoMohjang
{
    /// <summary>
    /// 画像の管理を行うクラス。
    /// 適切にキャッシュをとり、目的の画像の取得を補助する
    /// </summary>
    public class ImageCacheManager
    {
        private static ImageCacheManager cache;

        public const string MediaFolder = "Media\\";
        public const string HaiImageFolader = "Media\\hai";
        const int HaiTypeCount = 9 * 3 + 7;//牌の種類の数
        static string[] jihaiNames = {"ton.gif","nan.gif","sha.gif","pei.gif",
                                         "haku.gif","hatu.gif","tyun.gif"};

        private static string baseFolder;

        public static string BaseFolder
        {
            get { return ImageCacheManager.baseFolder; }
            set { ImageCacheManager.baseFolder = value; }
        }

        public static ImageCacheManager Cache
        {
            get {
                if (cache == null)
                {
                    cache = new ImageCacheManager();
                }
                return ImageCacheManager.cache;
            }
        }

        Image[] haiImageCache;
        Image[] yokoHaiImageCache; 

        private ImageCacheManager() {
            haiImageCache = new Image[HaiTypeCount];
            yokoHaiImageCache = new Image[HaiTypeCount];
            baseFolder = Application.StartupPath;
        }

        /// <summary>
        /// 指定したファイルのフルパスを取得する
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public string FindMediaFile(string filename)
        {
            string fullPath;
            fullPath = filename;
            if (File.Exists(fullPath)) return fullPath;

            fullPath = Path.Combine(baseFolder, filename);
            if (File.Exists(fullPath)) return fullPath;

            fullPath = Path.Combine(Path.Combine(baseFolder,MediaFolder), filename);
            if (File.Exists(fullPath)) return fullPath;

            fullPath = Path.Combine(Path.Combine(baseFolder, HaiImageFolader), filename);
            if (File.Exists(fullPath)) return fullPath;

            fullPath = Path.Combine(Environment.CurrentDirectory, filename);
            if (File.Exists(fullPath)) return fullPath;

            fullPath = Path.Combine(Path.Combine(Environment.CurrentDirectory, MediaFolder), filename);
            if (File.Exists(fullPath)) return fullPath;


            return null;

        }


        /// <summary>
        /// 牌の画像を取得する
        /// </summary>
        /// <param name="hai"></param>
        /// <param name="tate">縦か横かを指定</param>
        /// <returns></returns>
        public Image GetHaiImageOf(Hai hai, bool vertical)
        {
            if (vertical)
            {
                if (haiImageCache[hai.HaiIndex] == null)
                {
                    string filename = ToImageFile(hai,vertical);
                    string fullPath = FindMediaFile(filename);
                    haiImageCache[hai.HaiIndex] = Image.FromFile(fullPath);
                }
                return haiImageCache[hai.HaiIndex];
            }
            else
            {
                if (yokoHaiImageCache[hai.HaiIndex] == null)
                {
                    string filename = ToImageFile(hai, vertical);
                    string fullPath = FindMediaFile(filename);
                    yokoHaiImageCache[hai.HaiIndex] = Image.FromFile(fullPath);
                }
                return yokoHaiImageCache[hai.HaiIndex];
            }
        }
        /// <summary>
        /// 指定した牌の縦または横向きの画像のファイル名をを取得する
        /// フルパスではないので注意
        /// </summary>
        /// <param name="hai"></param>
        /// <param name="vertical">牌の向き</param>
        /// <returns></returns>
        public string ToImageFile(Hai hai,bool vertical)
        {

            string filename;
            switch (hai.Type)
            {
                case HaiType.Manzu:
                    filename = String.Format("man{0}.gif", hai.Number);
                    break;
                case HaiType.Pinzu:
                    filename = String.Format("pin{0}.gif", hai.Number);
                    break;
                case HaiType.Souzu:
                    filename = String.Format("sou{0}.gif", hai.Number);
                    break;

                case HaiType.Jihai:
                    filename = jihaiNames[hai.Number - 1];

                    break;
                default:
                    return null;
            }
            if (!vertical)
            {
                filename = "y" + filename;
            }
            return filename;

        }



    }
    public static class HaiMediaExtension
    {
        public static Image GetImage(this Hai hai)
        {
            return ImageCacheManager.Cache.GetHaiImageOf(hai, true);
        }

        public static Image[] GetMentsuImage(this Mentsu mentsu)
        {
            ImageCacheManager cache = ImageCacheManager.Cache;
            switch (mentsu.Type)
            {
                case MentsuType.Minko:
                case MentsuType.Syuntsu_M:
                case MentsuType.Minkan:
                    {
                        Image[] images = new Image[mentsu.Hais.Count];
                        for (int i = 0; i < images.Length; i++)
                        {
                            images[i] = cache.GetHaiImageOf(mentsu.Hais[i], true);
                        }
                        return images;
                    }

            }

            return null;
        }
    }
}
